package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.mapper.ProductSkuMapper;
import cn.wolfcode.shop.mapper.ProductSkuPropertyMapper;
import cn.wolfcode.shop.mapper.SkuPropertyMapper;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.shop.vo.ProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductSkuServiceImpl implements IProductSkuService {

    @Autowired
    private CatalogMapper catalogMapper;

    @Autowired
    private SkuPropertyMapper skuPropertyMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductSkuPropertyMapper productSkuPropertyMapper;



    @Override
    public List<Map> generateSku(GenerateSkuVo generateSkuVo) {
        String skuCodePrefix = generateSkuCodePrefix(generateSkuVo.getProduct());
        List<SkuProperty> skuPropertyList = generateSkuVo.getSkuPropertyList();
        List<Map> mapList = skuPropertyMapper.generateSku(generateSkuVo.getSkuPropertyList(), generateSkuVo.getSkuPropertyList().size());
        for (int i = 0; i < mapList.size(); i++) {
            Map map = mapList.get(i);
            map.put("code",skuCodePrefix+(i+1));
            map.put("price",generateSkuVo.getProduct().getBasePrice());
            for (int y = 0; y < skuPropertyList.size(); y++) {
                String skuPropertyId = skuPropertyList.get(y).getId().toString();
                map.put(skuPropertyId,map.get("skuProperty_"+skuPropertyId));
                map.remove("skuProperty_"+skuPropertyId);
            }
        }
        return mapList;
    }




    /**
     * 生成sku编号
     * @param product
     * @return
     */
    private String generateSkuCodePrefix(Product product){
        StringBuffer sb = new StringBuffer();
        List<Catalog> catalogList = catalogMapper.getAllParentCatalog(product.getCatalog().getId());
        catalogList.remove(0);
        //变量商品分类集合的,并且判断遍历出来的分类是否是一级分类
        for(int i=0;i<catalogList.size();i++){
            Catalog catalog = catalogList.get(i);
            if(i==0){
                //如果是一级分类就获取分类的编码
                sb.append(catalog.getCode().length() >2 ? catalog.getCode().substring(0,2) : catalog.getCode());
            }else{
                //否则就获取分类的排序号
                sb.append(catalog.getSort() > 9 ? catalog.getSort()+"" : "0"+catalog.getSort());
            }
        }
        return sb.toString()+product.getId();
    }

    public void save(ProductVo vo){
        vo.getProductSkuList().forEach(productSku -> {
            productSku.setProductId(vo.getProduct().getId());
            productSkuMapper.insert(productSku);
            productSku.getProductSkuPropertyList().forEach(productSkuProperty -> {
                productSkuProperty.setProductSkuId(productSku.getId());
                productSkuPropertyMapper.insert(productSkuProperty);
            });
        });
    }

    @Override
    public ProductSku getById(Long id) {
        return productSkuMapper.selectByPrimaryKey(id);
    }

}
